package com.example.boss.pinkdiary;

/**
 * Created by 打好boss on 2018/6/12.
 */


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boss.db.gen.diaryList;
import com.example.boss.db.gen.diaryTag;

import java.util.ArrayList;
import java.util.List;


public class diaryListAdapter extends RecyclerView.Adapter<diaryListAdapter.mViewHolder> {
    private List<diaryList> mDataList;
    private Context context;
    private int checkBox_Visibility;
    private ArrayList checkBoxList=new ArrayList();
    private String key;
    public interface ItemClickListener{
        void onItemClick(View view,int position,String lock);
        boolean onItemLongClick(View view,int position);
        void checkBoxClick(int position);
    }
    public ItemClickListener mListener;
    public void setOnItemClickListener(ItemClickListener listener){
        this.mListener=listener;
    }


    public diaryListAdapter(Context context,List<diaryList> list,String key) {
        this.context=context;
        mDataList = list;
//        this.checkBox_Visibility=checkBox_Visibility;
        this.key=key;
    }
    @Override
    public int getItemCount() {
        // 返回数据集合大小
        return mDataList == null ? 0 : mDataList.size();
    }
    @Override
    public void onBindViewHolder(final mViewHolder holder, final int position) {
        String s=mDataList.get(position).getDate();
        String s1[]=s.split("\\/");
        String s_year=s1[0];
        String s_month_day=s1[1];
        String s2[]=s_month_day.split("月");
        String s_month=s2[0];
        String s_day_s[]=s2[1].split("日");
        String s_day=s_day_s[0];
        String s_time=s1[2];
        final CheckBox checkBox=holder.checkBox;
        TextView t_day = holder.day;
        TextView t_month = holder.month;
        TextView t_year = holder.year;
        TextView t_tag = holder.tag;
        TextView t_title = holder.title;
        TextView t_content = holder.content;
        TextView t_time = holder.time;
        ImageView t_lock=holder.lock;
        t_day.setText(s_day);
        t_month.setText(s_month+"月/");
        t_year.setText(s_year);
        t_tag.setText(mDataList.get(position).getTag());

        t_time.setText(s_time);

        if(mDataList.get(position).getLock().equals("false")){
            t_lock.setVisibility(View.VISIBLE);
        }
        final Long id=mDataList.get(position).getId();
        final String lock=mDataList.get(position).getLock();

        String title=mDataList.get(position).getTitle().toString();
        String content=mDataList.get(position).getContent().toString();
        StringFormatUtil spanStr1,spanStr2;
        spanStr1 = new StringFormatUtil(context, title,key, R.color.colorPrimary).fillColor();
        spanStr2=new StringFormatUtil(context, content,key, R.color.colorPrimary).fillColor();
        if(spanStr1!=null){
            t_title.setText(spanStr1.getResult());
        }
        else{
            if(mDataList.get(position).getTitle().length()==0) t_title.setVisibility(View.GONE);
            else t_title.setText(mDataList.get(position).getTitle());
        }
        if(spanStr2!=null){
            t_content.setText(spanStr2.getResult());
        }
        else  t_content.setText(mDataList.get(position).getContent());


        if(checkBox_Visibility==1){
            checkBox.setVisibility(View.INVISIBLE);
        }
        if(mListener!=null){
            holder.checkBox.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    checkBoxList.add(mDataList.get(position).getDate());
                    mListener.checkBoxClick(position);
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mListener.onItemClick(holder.itemView, position,lock);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
                @Override
                public boolean onLongClick(View v){
//                    checkBox.setVisibility(View.VISIBLE);
                    int position = holder.getLayoutPosition();
                    mListener.onItemLongClick(holder.itemView, position);
                    return true;
                }
            });
        }
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new mViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false));
    }
    public class mViewHolder extends RecyclerView.ViewHolder {
        private TextView day;
        private TextView month;
        private TextView year;
        private TextView tag;
        private TextView title;
        private TextView content;
        private TextView time;
        private ImageView lock;
        private CheckBox checkBox;

        public mViewHolder(View itemView) {
            super(itemView);
            day = (TextView) itemView.findViewById(R.id.tv_day);
            month = (TextView) itemView.findViewById(R.id.tv_month);
            year = (TextView) itemView.findViewById(R.id.tv_year);
            tag = (TextView) itemView.findViewById(R.id.tv_tag);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            content = (TextView) itemView.findViewById(R.id.tv_content);
            time = (TextView) itemView.findViewById(R.id.tv_time);
            lock=(ImageView) itemView.findViewById(R.id.s_suo);
            checkBox=(CheckBox) itemView.findViewById(R.id.listview_select_cb);
        }
    }



}

